home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 017a / wininit.zip / WININIT.CPP next >
C/C++ Source or Header  |  1991-08-08  |  4KB  |  176 lines

  1. //    wininit.cpp - initialize windows desktop
  2.  
  3. /*    wininit reads the [WinInit] section of WIN.INI, and launches
  4.     programs as listed, moving their windows as indicated.
  5.  
  6.     WIN.INI format:
  7.  
  8.         [WinInit]
  9.         win1=Show X Y Dx Dy D:\path\name1.exe [args]
  10.         win2=Show X Y Dx Dy D:\path\name2.exe [args]
  11.         [...]
  12.  
  13.         win1 ... win63 = tags for windows (loaded in numerical order,
  14.                     NOT in the order found in WIN.INI)
  15.         Show = ShowWindow value (1=normal, 2=minimized[=icon])
  16.         X,Y = upper left corner coordinates
  17.         Dx,Dy = width and height (in pixels)
  18.  
  19.  
  20.     wininit should be the ONLY program named in window's "run="
  21.     and "load=" lines. By putting wininit.exe in the "load=" line,
  22.     the program manager window will appear normally; by putting
  23.     wininit.exe in the "run=" line, the program manager will appear
  24.     as an icon.
  25.  
  26.     This program has no windows - it merely launches the proper
  27.     programs and exits.
  28.  
  29.     Written 8/5/91 by Tom Roberts.
  30.  
  31.     Borland C++, ver 2.0. It has no resources or other files.
  32.  
  33.     Copyright 1991 by Tom Roberts.  All rights reserved.
  34. */
  35.  
  36. #include <windows.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40.  
  41. char Progname[] = "WinInit";
  42.  
  43. const int MAX_NAME = 80;
  44. const int MAX_ARGS = 128;
  45. const int MAX_WINDOWS = 64;
  46.  
  47. struct ProgInfo {
  48.     char name[MAX_NAME];
  49.     char args[MAX_ARGS];
  50.     int x,y,dx,dy;
  51.     int hwnd;
  52.     int show;
  53. };
  54.  
  55. ProgInfo Progs[MAX_WINDOWS];
  56.  
  57. int Inttok_err = 0;
  58.  
  59. //    inttok() - return integer value of a token
  60. //        (sets Inttok_err if error)
  61. int inttok(char *s, char *delim)
  62. {
  63.     char *p = strtok(s,delim);
  64.  
  65.     if(p && (isdigit(*p) || *p == '-'))
  66.         return atoi(p);
  67.     Inttok_err = 1;
  68.     return 0;
  69. }
  70.  
  71. void read_win_ini()
  72. {
  73.     char buf[256];
  74.     char key[8];
  75.  
  76.     memset(Progs,0,sizeof(Progs));
  77.  
  78.     for(int i=1; i<MAX_WINDOWS; ++i) {
  79.         Inttok_err = 0;
  80.         wsprintf(key,"win%d",i);
  81.         int j = GetProfileString(Progname,key,"",buf,sizeof(buf)-1);
  82.         if(j <= 0)
  83.             continue;
  84.         buf[sizeof(buf)-1] = '\0';
  85.         Progs[i].show = inttok(buf," \t\r\n");
  86.         Progs[i].x = inttok(NULL," \t\r\n");
  87.         Progs[i].y = inttok(NULL," \t\r\n");
  88.         Progs[i].dx = inttok(NULL," \t\r\n");
  89.         Progs[i].dy = inttok(NULL," \t\r\n");
  90.         char *p = strtok(NULL," \t\r\n");
  91.         if(p)
  92.             strcpy(Progs[i].name,p);
  93.         p = strtok(NULL,"\r\n");
  94.         if(p)
  95.             strcpy(Progs[i].args,p);
  96.         if(Inttok_err || Progs[i].name[0] == 0) {
  97.             Progs[i].name[0] = 0;
  98.             wsprintf(buf,"Error in WIN.INI line for '%s'",(LPSTR)key);
  99.             MessageBox(NULL,buf,Progname,MB_APPLMODAL|MB_OK|MB_ICONEXCLAMATION);
  100.         }
  101.     }
  102.  
  103.     return;
  104. }
  105.  
  106. int execute(char *program, char *args, int show)
  107. {
  108.     char buf[256];
  109.  
  110.     if(program[0] != '\\' && program[1] != ':') {
  111.         GetWindowsDirectory(buf,sizeof(buf));
  112.         if(buf[strlen(buf)-1] != '\\')
  113.             strcat(buf,"\\");
  114.     } else {
  115.         buf[0] = '\0';
  116.     }
  117.     strcat(buf,program);
  118.     if(args && *args) {
  119.         strcat(buf," ");
  120.         strcat(buf,args);
  121.     }
  122.  
  123.     int retval = WinExec(buf,show);
  124.     if(retval < 32) {
  125.         wsprintf(buf,"Cannot execute program\r\n'%s'",(LPSTR)program);
  126.         MessageBox(NULL,buf,Progname,MB_APPLMODAL|MB_OK|MB_ICONEXCLAMATION);
  127.         return 1;
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. //    name2hwnd() - given the program-name, finds the first window's hwnd
  134. HWND name2hwnd(char *name)
  135. {
  136.     char buf[MAX_NAME];
  137.     HWND hwnd;
  138.  
  139.     hwnd = GetWindow(GetDesktopWindow(),GW_CHILD);
  140.  
  141.     while(hwnd) {
  142.         GetModuleFileName(GetClassWord(hwnd,GCW_HMODULE),buf,MAX_NAME);
  143.         buf[MAX_NAME-1] = '\0';
  144.         int n = strlen(buf) - strlen(name);
  145.         if(n >= 0 && stricmp(buf+n,name) == 0)
  146.             break;
  147.         hwnd = GetNextWindow(hwnd,GW_HWNDNEXT);
  148.     }
  149.  
  150.     return hwnd;
  151. }
  152.  
  153.  
  154. #pragma argsused
  155. int PASCAL WinMain(HANDLE instance, HANDLE prev, LPSTR cmdline,
  156.                         int cmdshow)
  157. {
  158.     read_win_ini();
  159.  
  160.     // execute the programs
  161.     for(int i=1; i<MAX_WINDOWS; ++i) {
  162.         if(Progs[i].name[0] == 0)
  163.             continue;
  164.         if(execute(Progs[i].name,Progs[i].args,SW_HIDE))
  165.             continue;
  166.         Progs[i].hwnd = name2hwnd(Progs[i].name);
  167.         if(Progs[i].hwnd == 0)
  168.             continue;
  169.         MoveWindow(Progs[i].hwnd,Progs[i].x,Progs[i].y,
  170.                         Progs[i].dx,Progs[i].dy,TRUE);
  171.         ShowWindow(Progs[i].hwnd,Progs[i].show);
  172.     }
  173.  
  174.     return 0;
  175. }
  176.